home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / examples / fourier < prev    next >
Text File  |  1995-05-20  |  2KB  |  80 lines

  1. //
  2. // Demonstrate the effect of adding terms to a Fourier expansion
  3. //
  4.  
  5. // We want to approximate a square wave...
  6. // The Fourier Series for a square wave is a 
  7. // sum of odd harmonics - we will demonstrate.
  8.  
  9. // Start up a plot window...
  10.  
  11. //_plsori(1);
  12. plstart(2,2);
  13.  
  14. // Compute the 1st term in the Fourier series and plot.
  15.  
  16. t = (0:10:.1)';
  17. y = sin (t);
  18.  
  19. pltitle ("Fundamental Frequency");
  20. plwid(10);
  21. plfont(1);
  22. plot ( [t,y] );
  23. plptex ("Pen width = 10", 4, 0.4);
  24. plptex ("Font = 1 (Normal)", 4, 0.1);
  25. pause ();
  26.  
  27. // Now add the third harmonic to the fundamental, and plot.
  28.  
  29. y = sin (t) + sin (3*t)/3;
  30.  
  31. pltitle ("1st and 3rd Harmonics");
  32. plwid(7);
  33. plfont(4);
  34. plot ( [t,y] );
  35. plptex ("Pen width = 7", 4, 0.4);
  36. plptex ("Font = 4 (Script)", 4, 0.1);
  37. pause ();
  38.  
  39. // Now use the first, third, fifth, seventh, and ninth harmonics.
  40.  
  41. y = sin (t) + sin (3*t)/3 + sin (5*t)/5 + sin (7*t)/7 + sin (9*t)/9;
  42. pltitle ("1st, 3rd, 5th, 7th, 9th Harmonics");
  43. plwid(4);
  44. plfont(3);
  45. plot ( [t,y] ); 
  46. plptex ("Pen width = 3", 4, 0.4);
  47. plptex ("Font = 3 (Italic)", 4, 0.1);
  48. pause ();
  49.  
  50. //
  51. // Now create a matrix with rows that represent adding
  52. // more and more terms to the series.
  53. // 
  54.  
  55. t = (0:3.14:.02);
  56. y = zeros(10,max(size(t)));
  57. x = zeros(size(t));
  58.  
  59. for (k in 1:19:2)
  60. {
  61.   x = x + sin(k*t)/k; 
  62.   y[(k+1)/2;] = x;
  63. }
  64.  
  65. //
  66. // Now make a nice 3-D plot that shows the effect
  67. // of adding more and more terms to the series.
  68. //
  69.  
  70. plfont (2);
  71. plwid(1);
  72. pltitle ("Square Wave via Fourier Series");
  73. ylabel ("No. Terms");
  74. plaz (140);
  75. plmesh (<< x = t; y=1:10; z=y' >>);
  76. plptex ("Pen width = 1", -1, 4.5);
  77. plptex ("Font = 2 (Roman)", -1, 3.8);
  78.  
  79. # plprint ("p6.ps");    // Make hardcopy (Postscript default).
  80.